<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            /*素航哥哥到此一游 2025-4-29  16点24分*/
            background-color: green;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .register-container {
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 350px;
        }
        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 25px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }
        input[type="text"]:focus,
        input[type="password"]:focus {
            border-color: #007bff;
            outline: none;
        }
        .btn-register {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        .btn-register:hover {
            background-color: #0056b3;
        }
        .error-message {
            color: #dc3545;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }
        .success-message {
            color: #28a745;
            font-size: 14px;
            margin-top: 10px;
            text-align: center;
            display: none;
        }
    </style>
</head>
<body>
<div class="register-container">
    <h2>用户注册</h2>
    <form id="registerForm">
        <div class="form-group">
            <label for="userName">用户名</label>
            <input type="text" id="userName" name="userName" autocomplete="username" required>
            <div id="userNameError" class="error-message"></div>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" autocomplete="new-password" required>
            <div id="passwordError" class="error-message"></div>
        </div>
        <button type="submit" class="btn-register">注册</button>
        <div id="successMessage" class="success-message"></div>
    </form>
</div>
<script src="js/ww.js"></script>
<script>
    $(document).ready(function() {
        $('#registerForm').submit(function(e) {
            e.preventDefault();

            // 清除之前的错误消息
            $('.error-message').hide();

            // 获取表单数据
            const userName = $('#userName').val().trim();
            const password = $('#password').val().trim();

            // 简单的客户端验证
            let isValid = true;

            if (userName === '') {
                $('#userNameError').text('请输入用户名').show();
                isValid = false;
            } else if (userName.length < 4 || userName.length > 20) {
                $('#userNameError').text('用户名长度应在4-20个字符之间').show();
                isValid = false;
            }
            if (password === '') {
                $('#passwordError').text('请输入密码').show();
                isValid = false;
            } else if (password.length < 6) {
                $('#passwordError').text('密码长度至少为6个字符').show();
                isValid = false;
            }

            if (!isValid) {
                return;
            }

            // 准备发送的数据
            const userData = {
                "userName": userName,
                "password": password
            };

            // 发送AJAX请求
            $.ajax({
                url: 'http://localhost:8080/sysUser/save',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(userData),
                success: function(response) {
                    if (response.success) {
                        // 注册成功
                        $('#successMessage').text(response.message || '注册成功！').show();
                        $('#registerForm')[0].reset();

                        // 3秒后跳转到登录页面或其他页面
                        alert('注册成功!');
                        window.location.href='./index.html';
                    } else {
                        // 注册失败，显示服务器返回的错误消息
                        if (response.message) {
                            $('#userNameError').text(response.message || '注册失败，请稍后再试').show();
                        }
                    }
                },
                error: function(xhr, status, error) {
                    // 处理错误响应
                    let errorMessage = '注册过程中发生错误';
                    if (xhr.responseJSON && xhr.responseJSON.message) {
                        errorMessage = xhr.responseJSON.message;
                    } else if (xhr.status === 404) {
                        errorMessage = '服务器接口不存在，请检查网络连接';
                    } else if (xhr.status === 500) {
                        errorMessage = '服务器内部错误，请稍后再试';
                    }
                    $('#userNameError').text(errorMessage).show();
                }
            });
        });
    });
</script>
</body>
</html>